use std::sync::Arc;

use axum::{
    http::{header, HeaderValue},
    Extension, Router,
};
use tower::ServiceBuilder;
use tower_http::{
    cors::{Any, CorsLayer},
    set_header::SetResponseHeaderLayer,
};

use crate::{api, server::AppState};

pub fn router(app_state: AppState) -> Router {
    Router::new().nest("/", api::router()).layer(
        ServiceBuilder::new()
            .layer(
                CorsLayer::new()
                    .allow_origin(Any)
                    .allow_headers(Any)
                    .allow_methods(Any),
            )
            .layer(SetResponseHeaderLayer::overriding(
                header::SERVER,
                HeaderValue::from_static("EchoServer"),
            ))
            .layer(Extension(Arc::new(app_state))),
    )
}
